import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;


public class ChannelClient {
	
	public static void main(String[] args) throws Exception{
		int port = 8083;
		
		SocketChannel socketChannel = SocketChannel.open();
		socketChannel.configureBlocking(false);
		
		System.out.println("Init connection ");
		socketChannel.connect(new InetSocketAddress("localhost",port));
		
		while(!socketChannel.finishConnect()){
			doSthUseful();
		}
		
		System.out.println("connection established");
		ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
		socketChannel.read(byteBuffer);
		
		CharBuffer charBuffer = byteBuffer.asCharBuffer();
		while(charBuffer.hasRemaining()){
			System.out.println(charBuffer.get());
		}
		
		socketChannel.close();
	}

	private static void doSthUseful() {
		// TODO Auto-generated method stub
		System.out.println("doSthUseful");
	}

}
